﻿using UnityEngine;
using UnityEngine.UI;

public class ColorPicker : MonoBehaviour
{
    public Slider hSlider;
    public Slider sSlider;
    public Slider vSlider;

    public Image testImg;

    public static Color pickColor;

    private HsvColor hsvColor;

    void Start()
    {

        hSlider.onValueChanged.AddListener(this.ChangeHColor);
        sSlider.onValueChanged.AddListener(this.ChangeColor);
        vSlider.onValueChanged.AddListener(this.ChangeColor);
        SetHueColorBackForSlider();
        ChangeHColor(0);
        hsvColor = new HsvColor();
        ChangeColor(0);
        ChangeSScliderBack();
    }

    private void ChangeHColor(float arg0)
    {
        ChangeColor(arg0);
        ChangeSScliderBack();
        ChangeVScliderBack();
    }

    private void ChangeColor(float arg0)
    {
        hsvColor.H = hSlider.value * 360;
        hsvColor.S = sSlider.value;
        hsvColor.V = vSlider.value;
        Color color = HSVUtil.ConvertHsvToRgb(hsvColor.H, hsvColor.S, hsvColor.V);
        testImg.color = color;
        pickColor = color;
    }

    private void SetHueColorBackForSlider()
    {
        float w = 360, h = 20;
        Sprite hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(0, 0, w, h), Vector2.zero);
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                var pixColor = GetHueColor(x, 1, 1);
                hue_Sprite.texture.SetPixel(x, y, pixColor);
            }
        }

        hue_Sprite.texture.Apply();
        Image img = hSlider.transform.Find("Background").GetComponent<Image>();
        img.sprite = hue_Sprite;
        hSlider.value = 1;
    }

    private void ChangeVScliderBack()
    {
        float w = 50, h = 20;
        Sprite hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(0, 0, w, h), Vector2.zero);
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                var pixColor = GetHueColor(hsvColor.H, 1, x / w);
                hue_Sprite.texture.SetPixel(x, y, pixColor);
            }
        }

        hue_Sprite.texture.Apply();
        Image img = vSlider.transform.Find("Background").GetComponent<Image>();
        img.sprite = hue_Sprite;
    }

    private void ChangeSScliderBack()
    {
        float w = 50, h = 20;
        Sprite hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(0, 0, w, h), Vector2.zero);
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                var pixColor = GetHueColor(hsvColor.H, x / w, hsvColor.V);
                hue_Sprite.texture.SetPixel(x, y, pixColor);
            }
        }

        hue_Sprite.texture.Apply();
        Image img = sSlider.transform.Find("Background").GetComponent<Image>();
        img.sprite = hue_Sprite;
    }


    private static Color GetHueColor(float h,float s,float v)
    {
        HsvColor hsvColor = new HsvColor(h, s, v);
        return HSVUtil.ConvertHsvToRgb(hsvColor.H, s, v);
    }
}
